package com.qf.mpswaggershiro.config;

import com.qf.mpswaggershiro.utils.Constants;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.web.config.DefaultShiroFilterChainDefinition;
import org.apache.shiro.spring.web.config.ShiroFilterChainDefinition;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ShiroConfig {
    @Bean
    public ShiroFilterChainDefinition shiroFilterChainDefinition(){
        DefaultShiroFilterChainDefinition definition = new DefaultShiroFilterChainDefinition();
        //放行指定路径
        definition.addPathDefinition("/swagger-ui.html","anon");
        definition.addPathDefinition("/user/get/**","anon");
        definition.addPathDefinition("/login.html","anon");
       // definition.addPathDefinition("/**","authc");


        return definition;
    }

    @Bean
    public Realm realm(){
        CustomRealm customRealm = new CustomRealm();

        //设置密码处理方式
        HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();

        hashedCredentialsMatcher.setHashAlgorithmName("md5");
        hashedCredentialsMatcher.setHashIterations(Constants.HASH_TIMES);
        /*盐（salt）在CustomRealm这里处理*/

        customRealm.setCredentialsMatcher(hashedCredentialsMatcher);

        return customRealm;
    }

}
